import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @Author 12629
 * @Description：
 */
 class BinaryTree {

    static class TreeNode {
        public char val;
        public TreeNode left;//左孩子的引用
        public TreeNode right;//右孩子的引用

        public TreeNode(char val) {
            this.val = val;
        }
    }

    //public TreeNode root;

    /**
     * 创建一棵二叉树 返回这棵树的根节点
     * @return
     */
    public TreeNode createTree() {

        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }

    // 前序遍历
    public void preOrder(TreeNode root) {
        if(root == null) return;
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }


    /*
    遍历子路：
    List<Character> ret = new ArrayList<>();
    public List<Character> preorderTraversal(TreeNode root) {
        if(root == null) return ret;
        ret.add(root.val);
        preorderTraversal(root.left);
        preorderTraversal(root.right);
        return ret;
    }*/

    /**
     * 子问题思路：
     * @param root
     * @return
     */
    public List<Character> preorderTraversal(TreeNode root) {
        List<Character> ret = new ArrayList<>();
        if(root == null) return ret;
        ret.add(root.val);

        List<Character> leftTree = preorderTraversal(root.left);
        ret.addAll(leftTree);

        List<Character> rightTree = preorderTraversal(root.right);
        ret.addAll(rightTree);

        return  ret;
    }


    // 中序遍历
    void inOrder(TreeNode root){
        if(root == null) return;
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }

    public List<Character> inorderTraversal(TreeNode root) {
        List<Character> ret = new ArrayList<>();
        if(root == null) return ret;

        List<Character> leftTree = inorderTraversal(root.left);
        ret.addAll(leftTree);


        ret.add(root.val);


        List<Character> rightTree = inorderTraversal(root.right);
        ret.addAll(rightTree);

        return  ret;
    }

    // 后序遍历
    void postOrder(TreeNode root){
        if(root == null) return;
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }

    public List<Character> postorderTraversal(TreeNode root) {
        List<Character> ret = new ArrayList<>();
        if(root == null) return ret;

        List<Character> leftTree = postorderTraversal(root.left);
        ret.addAll(leftTree);

        List<Character> rightTree = postorderTraversal(root.right);
        ret.addAll(rightTree);

        ret.add(root.val);

        return  ret;
    }


    /**
     * 获取树中节点的个数
     */
    public static int size;
    public void size1(TreeNode root) {
        if(root==null){
            return ;
        }else{
            size++;
            size1(root.left);
            size1(root.right);
            return ;
        }
    }

    public int size(TreeNode root){
        int size = 0;
        if(root==null){
            return 0;
        }
            return size(root.left)+size(root.right)+1;
    }
    int getLeafNodeCount1(TreeNode root) {
        if(root==null){
            return 0;
        }
        if(root.left==null&&root.right==null){
            return 1;
        }else{
            return getLeafNodeCount1(root.right)+getLeafNodeCount1(root.left);
        }
    }
    public static int Size;
    void getLeafNodeCount2(TreeNode root){
        if(root==null){
            return ;
        }
        if(root.left==null&&root.right==null){
            Size++;
            return;
        }
        getLeafNodeCount2(root.left);
        getLeafNodeCount2(root.right);
    }
    public int Leve(TreeNode root,int k){
        if(root==null){
            return 0;
        }
        return k==1?1:Leve(root.left,k-1)+Leve(root.right,k-1);
    }

    public TreeNode find (TreeNode root,char val){
        if(root==null){
            return null;
        }
        if(root.val==val){
            return root;
        }
        TreeNode left = find(root.left,val);
        if(left!=null){
            return left;
        }else{
            return find(root.right,val) ;
        }
    }
    /*
获取第K层节点的个数
*/
    int getKLevelNodeCount(TreeNode root,int k) {
        if(root == null) return 0;
        if(k == 1) return 1;
        return getKLevelNodeCount(root.left,k-1) +
                getKLevelNodeCount(root.right,k-1);
    }

    //层序遍历
    void levelOrder(TreeNode root) {
        if(root == null) return ;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        List<List<Integer>> List = new ArrayList<>();
        while(!queue.isEmpty()){
            List<Integer> list = new ArrayList<>();
            TreeNode head = queue.poll();
            list.add(head.val);
            if(head.left!=null){
                queue.offer(head.left);
            }
            if(head.right!=null){
                queue.offer(head.right);
            }
            List.add(list);
        }
    }
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if(root==null){
            return null;
        }

        List<Integer> tem = postorderTraversal(root.left);
        list.addAll(tem);
        tem = postorderTraversal(root.right);
        list.addAll(tem);
        list.add(root.val);
        return list;

    }


    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();
    }

    }
}
